package com.android.cl.coludtf.service.asynctask;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;

import com.android.cl.coludtf.R;
import com.android.cl.coludtf.exception.CallJSONException;
import com.android.cl.coludtf.exception.NetworkAvailableException;
import com.android.cl.coludtf.model.BaseBO;
import com.android.cl.coludtf.model.Result;

import java.io.IOException;
import java.text.ParseException;

public class BaseAsyncTask extends AsyncTask<BaseBO, Long, Result> {
    private static final String TAG = "BaseAsyncTask";
    Context context;
    ProgressDialog progressDialog = null;
    String title;
    String content;

    public BaseAsyncTask() {

    }

    public BaseAsyncTask(Context context, String title, String content) {
        super();
        this.context = context;
        this.title = title;
        this.content = content;
    }

    public BaseAsyncTask(Context context) {
        super();
        this.context = context;
    }

    @Override
    protected Result doInBackground(BaseBO... params) {
        Result r = new Result();

        try {

            r = excuteTask(params);

        } catch (NetworkAvailableException e) {
            // 移动数据连接被禁用
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog.cancel();
            }

            r.setCode("500");
            r.setMessage("网络数据连接异常");
        } catch (CallJSONException e) {
            // 获取json数据出错
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog.cancel();
            }
            r.setCode("500");
            r.setMessage("获取数据出错");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (progressDialog != null)
            progressDialog.cancel();

        return r;
    }

    @Override
    protected void onPreExecute() {
        if (title != null && content != null
                && !((Activity) context).isFinishing()) {
            progressDialog = new ProgressDialog(context);
            progressDialog.setTitle(this.title);
            progressDialog.setMessage(this.content);
            progressDialog.setCancelable(true);
            if (context != null && progressDialog != null
                    && !progressDialog.isShowing())
                progressDialog.show();
        }

    }

    @Override
    protected void onPostExecute(Result result) {

        super.onPostExecute(result);
    }

    @Override
    protected void onProgressUpdate(Long... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onCancelled() {
        if (context != null && progressDialog != null) {
            progressDialog.cancel();
        }
        super.onCancelled();
    }

    public void showError(String title, String content) {
        if (context != null && progressDialog != null
                && progressDialog.isShowing()) {
            progressDialog.cancel();
        }
        AlertDialog.Builder build = new AlertDialog.Builder(context);
        build.setTitle(title).setMessage(content).setIcon(R.mipmap.alert2)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        ok();
                    }
                });
        if (!((Activity) context).isFinishing()) {
            build.show();
        }

    }

    void ok() {

    }

    Result excuteTask(BaseBO... params) throws NetworkAvailableException,
            CallJSONException, ParseException, IOException {
        return null;
    }

    ;
}
